// import CityData from "./assets/js/cityData.js";
export default {
    // inject: ['reload'],
    data() {
        return {
            dicData: {},

            // 承接详情状态列表
            undertakeStatusList: [
                {
                    step: 1,
                    status: 0,
                    supplyLabel: "待对方处理",
                    consumeLabel: "待处理"
                },
                {
                    step: 2,
                    status: 1,
                    supplyLabel: "待处理对方意见",
                    consumeLabel: "待对方确认意见"
                },
                {
                    step: 3,
                    status: 2,
                    supplyLabel: "待确认合同",
                    consumeLabel: "待对方确认合同"
                },
                {
                    step: 4,
                    status: 3,
                    supplyLabel: "待双方签字",
                    consumeLabel: "待双方签字"
                },
                {
                    step: 4,
                    status: 31,
                    supplyLabel: "待自己签字",
                    consumeLabel: "待对方签字"
                },
                {
                    step: 4,
                    status: 32,
                    supplyLabel: "待对方签字",
                    consumeLabel: "待自己签字"
                },
                {
                    step: 5,
                    status: 4,
                    supplyLabel: "合同签订成功",
                    consumeLabel: "合同签订成功"
                },
                {
                    step: 6,
                    status: 5,
                    supplyLabel: "合同已终止",
                    consumeLabel: "合同已终止"
                },
            ],

            // 订单状态
            orderStatusList: [
                {
                    id: 1,
                    supplyLabel: "待对方付款",
                    consumeLabel: "待付款"
                },
                {
                    id: 2,
                    supplyLabel: "待作业",
                    consumeLabel: "待作业"
                },
                {
                    id: 3,
                    supplyLabel: "待完成",
                    consumeLabel: "待完成"
                },
                {
                    id: 4,
                    supplyLabel: "待对方评价",
                    consumeLabel: "待评价"
                },
                {
                    id: 5,
                    supplyLabel: "已完成",
                    consumeLabel: "已完成"
                },
                {
                    id: 6,
                    supplyLabel: "对方已取消",
                    consumeLabel: "已取消"
                },
            ],

            // 合同状态
            contractStatusList: [
                {
                    id: 1,
                    supplyLabel: "待双方签订",
                    consumeLabel: "待双方签订"
                },
                {
                    id: 2,
                    supplyLabel: "待自己签字",
                    consumeLabel: "待对方签订"
                },
                {
                    id: 3,
                    supplyLabel: "待对方签订",
                    consumeLabel: "待自己签字"
                },
                {
                    id: 4,
                    supplyLabel: "签署成功",
                    consumeLabel: "签署成功"
                },
                {
                    id: 9,
                    supplyLabel: "合同取消",
                    consumeLabel: "合同取消"
                },
            ]
        }
    },
    computed: {
    },
    methods: {
        getDataListByKey(key) {
            let data = this
            let keys = key.split(".")
            keys.forEach(k => {
                if (data) data = data[k]
            })
            return data || []
        },

        /**
         * 生命周期函数--监听页面加载
         */
        getDataLabelById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find(item => {
                    return id && item.id && item.id.toString() === id.toString()
                })
                if (find) return find.label
            }
            return ""
        },

        /**
         * 根据索引值获取对应id
         */
        getIdByIndex(key, index) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find((item, position) => {
                    return index === position
                })
                if (find) return find.id
            }
            return ''
        },

        /**
         * 根据索引值获取对应label
         */
        getLabelByIndex(key, index) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find((item, position) => {
                    return index === position
                })
                if (find) return find.label
            }
            return ''
        },

        /**
         * 根据id获取对应value
         */
        getLabelById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let index = dataList.findIndex(item => {
                    return item.id && item.id.toString() === id.toString()
                })
                if (index > -1) return dataList[index].label
            }
            return ''
        },

        /**
         * 根据id值获取对应索引
         */
        getIndexById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let index = dataList.findIndex(item => {
                    return item.id && id &&  item.id.toString() === id.toString()
                })
                if (index > -1) return index
            }
            return null
        },

        /**
         * 查询字典数据
         * @param keys
         * @returns {Promise<unknown>}
         */
        getDictionaryList (keys) {
            let self = this
            return new Promise(function (resolve, reject) {
                self.$api.getChildDictionaryListByKeys({
                    keys: keys
                }).then(res => {
                    let dicData = {}
                    keys.forEach(key => {
                        let dataList = res[key] || []
                        dataList.forEach(item => {
                            item.id = item.key
                            item.label = item.name
                            delete item.key
                            delete item.name
                        })
                        dicData[key] = dataList
                    })
                    self.dicData = dicData
                    resolve()
                }).catch(e => {
                    reject()
                })
            })
        },

        /**
         * 获取承接状态
         * @param status
         * @returns {null}
         */
        getUndertakeStatusLabel (status, cooperateType) {
            let find = this.undertakeStatusList.find(item => {
                return item.status === status
            })
            if (!find) return null
            let label = null
            if ((this.userInfo.type === "supply" && cooperateType === 'undertake' ) || (this.userInfo.type === "consume" && cooperateType === 'cooperate' )) {
                label = find.supplyLabel
            } else {
                label = find.consumeLabel
            }
            return label
        },

        /**
         * 获取订单状态
         * @param status
         * @returns {null}
         */
        getOrderStatusLabel (status) {
            let find = this.orderStatusList.find(item => {
                return item.id === status
            })
            if (!find) return null
            let label = null
            if (this.userInfo.type === "supply") {
                label = find.supplyLabel
            } else if (this.userInfo.type === "consume") {
                label = find.consumeLabel
            }
            return label
        },

        /**
         * 获取合同状态
         * @param status
         * @returns {null}
         */
        getContractStatusLabel (status) {
            let find = this.contractStatusList.find(item => {
                return item.id === status
            })
            if (!find) return null
            let label = null
            if (this.userInfo && this.userInfo.type === "supply") {
                label = find.supplyLabel
            } else if (this.userInfo && this.userInfo.type === "consume") {
                label = find.consumeLabel
            }
            return label
        }
    }
}
